Haskell Yoneda,CoYoneda
理解しようと思うと相当な量の知識が必要っぽい
code:Yoneda.hs
{-# LANGUAGE Rank2Types #-} -- | @Yoneda f a@ can be viewed as the partial application of 'fmap' to its second argument.
newtype Yoneda f a = Yoneda { runYoneda :: forall b. (a -> b) -> f b }
Yonedaは関数の外側にforallの型変数が1つしか出ていないので、Rank2Typesを有効にすれば問題ないらしい
ここでいう関数の外側はforall bのb
runYonedaはfmap :: (a -> b) -> f a -> f bの f aを抜かした形
code:Coyoneda.hs
{-# LANGUAGE RankNTypes #-} data Coyoneda f a where
Coyoneda :: (b -> a) -> f b -> Coyoneda f a
data Coyoneda f a where Coyoneda :: ...という書き方もGADTs由来の記法
Coyonedaはb部分を見たときにn個の型が問題に
Coyoneda :: (b -> a) -> f b -> Coyoneda f aをa = Int, b = Intで具体化してみる。
code:Coyoneda.hs
(Int -> Int) -> f Int -> Coyoneda f Int
fにはMaybe、リストの[]とかIdentityなどが入る
Maybe なら
(Int -> Int) -> Maybe Int -> Coyoneda Maybe Int
[] なら
(Int -> Int) -> [Int] -> Coyoneda [] Int
Identity なら
(Int -> Int) -> Identity Int -> Coyoneda Identity Int
Functor が無い型でも (Int -> Int) -> f Int -> Coyoneda f Int は組める
確認用
Q.
関連
参考
メモ